## creates table for comparing opinion gaps
## across various cleavages
## table 5

rm(list=ls())

library(tidyverse)
library(kableExtra)

# read in data

opinion <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_noleaners_allcleavages.RDS')

issuetopics <- opinion$issuetopics
opinion <- opinion[names(opinion) != 'issuetopics']

calculate_cleavage <- function(df1, df2) {
  df <- abs(df1 - df2)
  df <- df %>% 
    as.data.frame() %>% 
    rownames_to_column('question')
  
  df <- df %>% 
    left_join(issuetopics %>% select(question, topic_6), by = 'question') %>% 
    bind_rows(df %>% 
                mutate(topic_6 = 'all')) %>% 
    drop_na() %>% 
    gather(key = sim, value = opinion, -question, -topic_6) %>% 
    group_by(sim, topic_6) %>% 
    summarize(pol = mean(opinion)) %>% 
    ungroup() %>% 
    group_by(topic_6) %>% 
    summarize(pol_mean = mean(pol),
              pol_95lo = quantile(sort(pol), 0.025),
              pol_95hi = quantile(sort(pol), 0.975))
  
  return(df)
}

cleave_list <- list()

for (p in c('all', 'rep', 'dem')) {
  print(p)
  
  # age
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_ageold_boot')]], opinion[[paste0(p, '_ageyoung_boot')]])
  tmp.df$cleavage <- 'age_oldyoung'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  # educ
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_educhs_boot')]], opinion[[paste0(p, '_educcoll_boot')]])
  tmp.df$cleavage <- 'educ_collhs'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_educnohs_boot')]], opinion[[paste0(p, '_educhs_boot')]])
  tmp.df$cleavage <- 'educ_hsnohs'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  # class
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_inctop_boot')]], opinion[[paste0(p, '_incbottom_boot')]])
  tmp.df$cleavage <- 'class_topbottom'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_inctop_boot')]], opinion[[paste0(p, '_incmiddle_boot')]])
  tmp.df$cleavage <- 'class_topmiddle'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  
  # race 
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_racewhite_boot')]], opinion[[paste0(p, '_raceblack_boot')]])
  tmp.df$cleavage <- 'race_whiteblack'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_racewhite_boot')]], opinion[[paste0(p, '_racehisp_boot')]])
  tmp.df$cleavage <- 'race_whitehisp'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_racehisp_boot')]], opinion[[paste0(p, '_raceblack_boot')]])
  tmp.df$cleavage <- 'race_blackhisp'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  
  # religion
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_religbornagain_boot')]], opinion[[paste0(p, '_relignotbornagain_boot')]])
  tmp.df$cleavage <- 'relig_bornagain'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_religchurchweekly_boot')]], opinion[[paste0(p, '_religchurchnever_boot')]])
  tmp.df$cleavage <- 'relig_church'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  
  # sex
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_sexfemale_boot')]], opinion[[paste0(p, '_sexmale_boot')]])
  tmp.df$cleavage <- 'sex'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  # urbanrural
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_urban_boot')]], opinion[[paste0(p, '_rural_boot')]])
  tmp.df$cleavage <- 'urbanrural'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
}

cleavages <- bind_rows(cleave_list)

cleave_sum <- cleavages %>% 
  select(topic_6, party, cleavage, pol_mean) %>% 
  spread(key = topic_6, value = pol_mean) %>% 
  arrange(party, all)

cleave_sum %>% 
  mutate(cleavage = case_when(cleavage == 'educ_hsnohs' ~ 'Education: H.S. vs. No H.S.',
                              cleavage == 'class_topmiddle' ~ 'Class: Top vs. Middle',
                              cleavage == 'race_blackhisp' ~ 'Race: Black vs. Latino',
                              cleavage == 'sex' ~ 'Sex',
                              cleavage == 'educ_collhs' ~ 'Education: College vs. H.S.',
                              cleavage == 'class_topbottom' ~ 'Class: Top vs. Bottom',
                              cleavage == 'age_oldyoung' ~ 'Age: 65+ vs. Under 30',
                              cleavage == 'race_whitehisp' ~ 'Race: White vs. Latino',
                              cleavage == 'urbanrural' ~ 'Urban vs. Rural',
                              cleavage == 'race_whiteblack' ~ 'Race: White vs. Black',
                              cleavage == 'relig_bornagain' ~ 'Religion: Born Again',
                              cleavage == 'relig_church' ~ 'Religion: Church Attendance'),
         party = case_when(party == 'all' ~ 'All Respondents',
                           party == 'dem' ~ 'Democrats',
                           party == 'rep' ~ 'Republicans')) %>% 
  arrange(party, -all) %>% 
  select(party, cleavage, all, foreignpolicy, socialwelfare, economic, cultural, immigration, lawenforcement) %>% 
  mutate(party = cell_spec(party, angle = 90, format = 'latex')) %>% 
  kable('latex', booktabs = TRUE, digits = 2, escape = FALSE,
        col.names = linebreak(c(' ', 'Cleavage', 'All', 'Foreign\npolicy', 'Social\nwelfare', 
                                'Economic', 'Cultural', 'Immig.', 'Law\nenf.'), 
                              align = 'c'),
        align = c('l', 'l', rep('c', ncol(.)-2))) %>%
  add_header_above(c(' ' = 2, 'Policy Domains' = 7)) %>% 
  collapse_rows(1, latex_hline = 'major') %>% 
  row_spec(c(7, 13, 27), bold = TRUE) %>% 
  save_kable('tables/compare_cleavages_tab.tex')
